package com.finace.money.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.ParamJsonUtil;
import com.finace.money.common.util.PopuUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.AccountInfoBean;
import com.finace.money.data.bean.QryPositionBean;
import com.finace.money.data.bean.StatusListBean;
import com.finace.money.data.bean.TickBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.ConnManager;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.ui.adapter.KViewFragmenAdapter;
import com.finace.money.ui.fragment.BaseFragment;
import com.finace.money.ui.fragment.KLineViewFragment;
import com.finace.money.ui.fragment.MinutesViewFragment;
import com.finace.money.ui.widget.MyPopupWindow;
import com.finace.money.ui.widget.MyViewPager;

import java.util.ArrayList;
import java.util.TreeMap;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/11/27.
 */

public class StatusDetalActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.tv_head_title2)
    TextView tvHeadTitle2;
    @BindView(R.id.tv_right_title)
    TextView tvRightTitle;
    @BindView(R.id.viewPager_line)
    MyViewPager viewPagerLine;
    @BindView(R.id.radioGroup_k)
    RadioGroup radioGroupK;
    @BindView(R.id.tv_join_choose)
    TextView tvJoinChoose;
    @BindView(R.id.ll_add_my_choose)
    LinearLayout llAddMyChoose;
    @BindView(R.id.ll_deal_right_now)
    LinearLayout llDealRightNow;
    private ImageView ivClose;
    private ImageView ivPriceMinus;
    private EditText etPopupPrice;
    private ImageView ivPricePlus;
    private ImageView ivCountMinus;
    private ImageView ivCountPlus;
    private EditText etPopupCount;
    private Button buttonBuy;
    private Button buttonSale;
    private MyPopupWindow myPopupWindow;
    private StatusListBean mStatusListBean;
    private int mRangeCount = 1;
    private double m_tickSize = -1;
    private TickBean mPayloadBean;
    private String mProductID;
    private String mAccount;
    private ArrayList<QryPositionBean> mQryPositionLists;
    int mDuoTodayCount = 0;
    int mKongTodayCount = 0;
    int mDuoYesTodayCount = 0;
    int mKongYesTodayCount = 0;
    int maxCount = 0;
    private ArrayList<QryPositionBean> mHaveList;
    private String mSide;
    private String mOffset_flag;
    private int mRadioPosition = 0;//区分开仓 平今 平仓
    private int mEtCount = 0;//处理数量

    public StatusListBean getmStatusListBean() {
        return mStatusListBean;
    }

    @Override
    public int setLayout() {
        return R.layout.activity_status_detail;
    }

    @Override
    public void init() {

        initView();
        ((RadioButton) radioGroupK.getChildAt(0)).setChecked(true);
        viewPagerLine.setCurrentItem(0);
        viewPagerLine.setOffscreenPageLimit(1);
//        viewPagerLine.setScanScroll(false);//禁止左右滑动
        viewPagerLine.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ((RadioButton) radioGroupK.getChildAt(position)).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_TICK);
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter);
        unregisterReceiver(broadcastReceiver);
    }

    private void initView() {

        rlLeft.setOnClickListener(this);
        llAddMyChoose.setOnClickListener(this);
        llDealRightNow.setOnClickListener(this);
        tvHeadTitle2.setVisibility(View.VISIBLE);
        tvRightTitle.setVisibility(View.VISIBLE);

        mStatusListBean = (StatusListBean) getIntent().getSerializableExtra("statusData");
        m_tickSize = mStatusListBean.getM_Body().getM_TickSize();
        mProductID = mStatusListBean.getM_Body().getM_ProductID();
        String name = mStatusListBean.getM_Body().getM_ProductName();
        mPayloadBean = mStatusListBean.getM_Body().getTickbean();
        mAccount = ACache.get(UIUtil.getContext()).getAsString(Constant.account);
        showChoose(!mStatusListBean.getCheck());
        tvHeadTitle.setText(name);
        tvHeadTitle2.setText(mStatusListBean.getM_Body().getM_ProductID());
        tvRightTitle.setText(R.string.yixi);
        Drawable drawable = UIUtil.getDrawable(R.drawable.icon_avatar_left);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvRightTitle.setCompoundDrawables(drawable, null, null, null);
        tvRightTitle.setCompoundDrawablePadding(UIUtil.dip2px(2));
        ArrayList<BaseFragment> pages = new ArrayList<>();
        pages.add(new MinutesViewFragment());
        pages.add(new KLineViewFragment());

        KViewFragmenAdapter kViewFragmenAdapter = new KViewFragmenAdapter(getSupportFragmentManager(), pages);
        viewPagerLine.setAdapter(kViewFragmenAdapter);
    }

    private void showChoose(boolean isJoin) {
        Drawable drawable;
        if (isJoin) {
            drawable = UIUtil.getDrawable(R.drawable.icon_add);
            tvJoinChoose.setText(R.string.chosse_join);

        } else {
            drawable = UIUtil.getDrawable(R.drawable.icon_delete);
            tvJoinChoose.setText(R.string.choose_remove);
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvJoinChoose.setCompoundDrawables(drawable, null, null, null);
        tvJoinChoose.setCompoundDrawablePadding(UIUtil.dip2px(5));
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            TickBean tickBean = (TickBean) intent.getSerializableExtra(PushReceiver.DATA);
            if (null != tickBean && mStatusListBean.getM_Body().getM_ProductID().equals(tickBean.getSymbol())) {
                mPayloadBean = tickBean;
            }
        }
    };

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_left:
                onBackPressed();
                break;
            case R.id.ll_add_my_choose:
                ArrayList<StatusListBean> chooseLists = (ArrayList<StatusListBean>) ACache.get(UIUtil.getContext()).getAsObject(Constant.CHOOSE_ALL);
                if(null == chooseLists){
                    chooseLists = new ArrayList<>();
                }
                mStatusListBean.setCheck(!mStatusListBean.getCheck());
                showChoose(!mStatusListBean.getCheck());
                if (mStatusListBean.getCheck()) {
                    chooseLists.add(mStatusListBean);
                } else {
                    int h = -1;
                    for (int i = 0; i < chooseLists.size(); i++) {
                        if (chooseLists.get(i).getM_Body().getM_ProductID().equals(mStatusListBean.getM_Body().getM_ProductID())) {
                            h = i;
                            break;
                        }
                    }
                    if (h != -1) {
                        chooseLists.remove(h);
                    }
                }
                ACache.get(UIUtil.getContext()).put(Constant.CHOOSE_ALL, chooseLists);
                break;
            case R.id.ll_deal_right_now:
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra(Constant.main,2);
                intent.putExtra("id",mProductID);
                startActivity(intent);
                finish();
//                goBuyPopup();
                break;
            case R.id.iv_close:
                myPopupWindow.dismiss();
                break;
            case R.id.iv_price_minus:
                //价格减减
                if (null == etPopupPrice) {
                    return;
                }
                if (m_tickSize == -1) {
                    return;
                }

                String currentPrice = etPopupPrice.getText().toString();
                if (StringUtil.isNotNullString(currentPrice)) {
                    double newPrice = Double.valueOf(currentPrice) - m_tickSize;
                    if (newPrice <= m_tickSize) {
                        newPrice = m_tickSize;
                    }
                    etPopupPrice.setText(String.valueOf(newPrice));
                    buttonBuy.setText(String.valueOf(newPrice) + getString(R.string.buy));
                    buttonSale.setText(String.valueOf(newPrice) + getString(R.string.sale));
                } else {
                    etPopupPrice.setText(String.valueOf(m_tickSize));
                    buttonBuy.setText(String.valueOf(m_tickSize) + getString(R.string.buy));
                    buttonSale.setText(String.valueOf(m_tickSize) + getString(R.string.sale));
                }
                break;
            case R.id.iv_price_plus:
                //价格加加
                if (m_tickSize == -1) {
                    return;
                }
                if (null == etPopupPrice) {
                    return;
                }
                String currentPrice2 = etPopupPrice.getText().toString();
                if (StringUtil.isNotNullString(currentPrice2)) {
                    double newPrice = Double.valueOf(currentPrice2) + m_tickSize;
                    etPopupPrice.setText(String.valueOf(newPrice));
                    buttonBuy.setText(String.valueOf(newPrice) + getString(R.string.buy));
                    buttonSale.setText(String.valueOf(newPrice) + getString(R.string.sale));
                } else {
                    etPopupPrice.setText(String.valueOf(m_tickSize));
                    buttonBuy.setText(String.valueOf(m_tickSize) + getString(R.string.buy));
                    buttonSale.setText(String.valueOf(m_tickSize) + getString(R.string.sale));
                }
                break;
            case R.id.iv_count_minus:
                //手数减减
                if (null == etPopupCount) {
                    return;
                }
                String currentCount2 = etPopupCount.getText().toString();

                if (StringUtil.isNotNullString(currentCount2)) {
                    int newCount = Integer.valueOf(currentCount2) - mRangeCount;
                    if (newCount <= 0) {
                        newCount = 1;
                    }
                    etPopupCount.setText(String.valueOf(newCount));
                } else {
                    etPopupCount.setText("1");
                }
                break;
            case R.id.iv_count_plus:
                //手数加加
                if (null == etPopupCount) {
                    return;
                }
                String currentCount = etPopupCount.getText().toString();

                if (StringUtil.isNotNullString(currentCount)) {
                    int newCount = Integer.valueOf(currentCount) + mRangeCount;
                    if (maxCount != 0 && newCount >= maxCount) {
                        newCount = maxCount;
                    }
                    etPopupCount.setText(String.valueOf(newCount));
                } else {
                    etPopupCount.setText("1");
                }

                break;

            case R.id.button_buy:
                //买入
                goDeal(true);
                myPopupWindow.dismiss();
                break;
            case R.id.button_sale:
                //卖出
                goDeal(false);
                myPopupWindow.dismiss();
                break;

        }
    }

    /**
     * 下单
     *
     * @param b 区分买卖
     */
    private void goDeal(boolean b) {
        String count = etPopupCount.getText().toString().trim();
        if("".equals(count)){
            UIUtil.showToastSafe(getString(R.string.count_input_please));
            return;
        }
        if (b) {
            mSide = "true";
        } else {
            mSide = "false";
        }
        switch (mRadioPosition) {
            case 0://开仓
                mOffset_flag = "OPEN";
                mEtCount = Integer.valueOf(count);
                deal();
                break;
            case 1://平今
                if (b) {
                    //平空
                    if (Integer.valueOf(count) == mKongTodayCount) {
                        //今统统清掉
                        mOffset_flag = "CLOSETODAY";
                        mEtCount = mKongTodayCount;
                        deal();
                    } else if (Integer.valueOf(count) > mKongTodayCount) {
                        UIUtil.showToastSafe(getString(R.string.only) + mKongTodayCount + getString(R.string.clear_count_toady));
                        return;
                    } else {
                        mOffset_flag = "CLOSETODAY";
                        mEtCount = Integer.valueOf(count);
                        deal();
                    }
                } else {
                    //平多
                    if (Integer.valueOf(count) == mDuoTodayCount) {
                        //今统统清掉
                        mOffset_flag = "CLOSETODAY";
                        mEtCount = mDuoTodayCount;
                        deal();
                    } else if (Integer.valueOf(count) > mDuoTodayCount) {
                        UIUtil.showToastSafe(getString(R.string.only) + mDuoTodayCount + getString(R.string.clear_count_toady));
                        return;
                    } else {
                        mOffset_flag = "CLOSETODAY";
                        mEtCount = Integer.valueOf(count);
                        deal();
                    }
                }

                break;
            case 2://平仓
                if (b) {
                    //平空
                    if (Integer.valueOf(count) == (mKongYesTodayCount + mKongTodayCount)) {
                        //昨今统统清掉
                        mOffset_flag = "CLOSEYESTERDAY";
                        mEtCount = mKongYesTodayCount;
                        deal();
                        mOffset_flag = "CLOSETODAY";
                        mEtCount = mKongTodayCount;
                        deal();
                    } else if (Integer.valueOf(count) > mKongYesTodayCount) {
                        if (mKongYesTodayCount != 0) {
                            UIUtil.showToastSafe(getString(R.string.only) + mKongYesTodayCount + getString(R.string.clear_count_yestoady));
                            return;
                        } else {
                            if (Integer.valueOf(count) > mKongTodayCount) {
                                UIUtil.showToastSafe(getString(R.string.only) + mKongTodayCount + getString(R.string.clear_count_toady));
                                return;
                            } else {
                                mOffset_flag = "CLOSETODAY";
                                mEtCount = Integer.valueOf(count);
                                deal();
                            }
                        }
                    } else if (Integer.valueOf(count) < mKongYesTodayCount) {
                        mOffset_flag = "CLOSEYESTERDAY";
                        mEtCount = Integer.valueOf(count);
                        deal();
                    }
                } else {
                    //平多
                    if (Integer.valueOf(count) == (mDuoYesTodayCount + mDuoTodayCount)) {
                        //昨今统统清掉
                        mOffset_flag = "CLOSEYESTERDAY";
                        mEtCount = mDuoYesTodayCount;
                        deal();
                        mOffset_flag = "CLOSETODAY";
                        mEtCount = mDuoTodayCount;
                        deal();
                    } else if (Integer.valueOf(count) > mDuoYesTodayCount) {
                        if (mDuoYesTodayCount != 0) {
                            UIUtil.showToastSafe(getString(R.string.only) + mDuoYesTodayCount + getString(R.string.clear_count_yestoady));
                            return;
                        } else {
                            if (Integer.valueOf(count) > mDuoTodayCount) {
                                UIUtil.showToastSafe(getString(R.string.only) + mDuoTodayCount + getString(R.string.clear_count_toady));
                                return;
                            } else {
                                mOffset_flag = "CLOSETODAY";
                                mEtCount = Integer.valueOf(count);
                                deal();
                            }
                        }

                    } else if (Integer.valueOf(count) < mDuoYesTodayCount) {
                        mOffset_flag = "CLOSEYESTERDAY";
                        mEtCount = Integer.valueOf(count);
                        deal();
                    }
                }
                break;
        }

    }

    private void deal() {
        if (mEtCount != 0) {
            TreeMap<String, String> dealDetailMap = new TreeMap<>();
            dealDetailMap.put("account", mAccount);
            dealDetailMap.put("symbol", mProductID);
            dealDetailMap.put("size", String.valueOf(mEtCount));
            dealDetailMap.put("price", etPopupPrice.getText().toString());
            dealDetailMap.put("side", mSide);
            dealDetailMap.put("offset_flag", mOffset_flag);
            TreeMap<String, String> dealDetail = new TreeMap<>();
            dealDetail.put("msgtype", "PlaceOrderRequest");
            dealDetail.put("payload", ParamJsonUtil.getjsonData(dealDetailMap).toString());
            ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(dealDetail).toString());
        }
    }

    private void goBuyPopup() {
        mDuoTodayCount = 0;
        mKongTodayCount = 0;
        mDuoYesTodayCount = 0;
        mKongYesTodayCount = 0;
        mRadioPosition = 0;
        maxCount = 0;
        mQryPositionLists = (ArrayList<QryPositionBean>) ACache.get(UIUtil.getContext()).getAsObject(PushReceiver.QRY_POSITION_END);
        if(null == mQryPositionLists){
            return;
        }
        myPopupWindow = new PopuUtil().initAtLocationPopu(this, R.layout.popup_status_detail, llContent, Gravity.BOTTOM, 0, 0);
        TextView tvBalance = myPopupWindow.getContentView().findViewById(R.id.tv_balance);
        ivClose = myPopupWindow.getContentView().findViewById(R.id.iv_close);
        ivPriceMinus = myPopupWindow.getContentView().findViewById(R.id.iv_price_minus);
        etPopupPrice = myPopupWindow.getContentView().findViewById(R.id.et_popup_price);
        ivPricePlus = myPopupWindow.getContentView().findViewById(R.id.iv_price_plus);
        ivCountMinus = myPopupWindow.getContentView().findViewById(R.id.iv_count_minus);
        etPopupCount = myPopupWindow.getContentView().findViewById(R.id.et_popup_count);
        ivCountPlus = myPopupWindow.getContentView().findViewById(R.id.iv_count_plus);
        RadioGroup rgs = myPopupWindow.getContentView().findViewById(R.id.rgs_detail);
        RadioButton buttonOpenRoom = myPopupWindow.getContentView().findViewById(R.id.rb_open_room);
        RadioButton buttonCleanRoomToday = myPopupWindow.getContentView().findViewById(R.id.rb_clean_room_today);
        RadioButton buttonCleanRoom = myPopupWindow.getContentView().findViewById(R.id.rb_clean_room);
        buttonBuy = myPopupWindow.getContentView().findViewById(R.id.button_buy);
        buttonSale = myPopupWindow.getContentView().findViewById(R.id.button_sale);

        mHaveList = new ArrayList<>();
        for (QryPositionBean qryPositionBean : mQryPositionLists) {
            if (qryPositionBean.getPosition().getSymbol().equals(mProductID)) {
                mHaveList.add(qryPositionBean);
            }
        }
        if (mHaveList.size() > 0) {//持仓有没有
            buttonCleanRoom.setEnabled(true);
        } else {
            buttonCleanRoom.setEnabled(false);
        }
        controlCount();
        if (mStatusListBean.getM_Body().getM_ExchangeID().equals("SHFE")) {//上期所例外
            buttonCleanRoomToday.setEnabled(true);
            if ((mDuoYesTodayCount + mKongYesTodayCount) != 0) {
                buttonCleanRoom.setEnabled(true);
            } else {
                buttonCleanRoom.setEnabled(false);
            }
        } else {
            buttonCleanRoomToday.setEnabled(false);
            buttonCleanRoom.setEnabled(true);
        }


        ivPriceMinus.setOnClickListener(this);
        ivPricePlus.setOnClickListener(this);
        ivCountMinus.setOnClickListener(this);
        ivCountPlus.setOnClickListener(this);
        ivClose.setOnClickListener(this);
        buttonBuy.setOnClickListener(this);
        buttonSale.setOnClickListener(this);

        AccountInfoBean accountInfoBean = (AccountInfoBean) ACache.get(UIUtil.getContext()).getAsObject(Constant.ACCOUNT_INFO);
        if (null != accountInfoBean) {
            SpannableString spannableString = StringUtil.getSpannableString(UIUtil.getString(R.string.balance_account) + "  "+accountInfoBean.getAvabileFunds(), 0, UIUtil.getString(R.string.balance_account).length(),
                    R.style.text_white899198_14, R.style.text_white_18);
            tvBalance.setText(spannableString);
        }
        if (null != mPayloadBean) {
//            buttonBuy.setText(mPayloadBean.getAsk() +"\n买入");
//            buttonSale.setText(mPayloadBean.getBid()+"\n卖出");
            buttonBuy.setText(mPayloadBean.getTrade() + getString(R.string.buy));
            buttonSale.setText(mPayloadBean.getTrade() + getString(R.string.sale));
            etPopupPrice.setText(mPayloadBean.getTrade());
            etPopupCount.setText("1");
        }
        etPopupCount.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (maxCount != 0 && !"".equals(etPopupCount.getText().toString()) && Integer.valueOf(etPopupCount.getText().toString()) > maxCount) {
                    etPopupCount.setText(String.valueOf(maxCount));
                }
            }
        });
        etPopupPrice.addTextChangedListener(new TextWatcher() {

            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (StringUtil.isNotNullString(etPopupPrice.getText().toString())) {
                    selectionStart = etPopupPrice.getSelectionStart();
                    selectionEnd = etPopupPrice.getSelectionEnd();
                    if (!StringUtil.isOnlyPointNumber(etPopupPrice.getText().toString())) {
                        UIUtil.showToastSafe(getString(R.string.double_two_tip));
                        //删除多余输入的字（不会显示出来）
                        editable.delete(selectionStart - 1, selectionEnd);
                        etPopupPrice.setText(editable);
                    }
                }
            }
        });
        rgs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedID) {
                switch (checkedID) {
                    case R.id.rb_open_room:
                        mRadioPosition = 0;
                        maxCount = 0;
                        break;
                    case R.id.rb_clean_room_today:
                        mRadioPosition = 1;
                        etPopupCount.setText("1");
                        //多和空取最大值
                        if ((mDuoTodayCount + mDuoYesTodayCount) >= (mKongTodayCount + mKongYesTodayCount)) {
                            maxCount = mDuoTodayCount + mDuoYesTodayCount;
                        } else {
                            maxCount = mKongTodayCount + mKongYesTodayCount;
                        }
                        break;
                    case R.id.rb_clean_room:
                        mRadioPosition = 2;
                        etPopupCount.setText("1");
                        if ((mDuoTodayCount + mDuoYesTodayCount) >= (mKongTodayCount + mKongYesTodayCount)) {
                            maxCount = mDuoTodayCount + mDuoYesTodayCount;
                        } else {
                            maxCount = mKongTodayCount + mKongYesTodayCount;
                        }
                        break;
                }
            }
        });

    }

    /**
     * 计算今昨多空
     */
    private void controlCount() {
        if(mHaveList.size()>0){
            for (QryPositionBean haBean : mHaveList) {
                if (haBean.getPosition().getYd_position() != 0) {
                    if (haBean.getPosition().isSide()) {
                        mDuoYesTodayCount += haBean.getPosition().getYd_position();
                    } else {
                        mKongYesTodayCount += haBean.getPosition().getYd_position();
                    }
                } else {
                    if (haBean.getPosition().isSide()) {
                        mDuoTodayCount += haBean.getPosition().getToday_position();
                    } else {
                        mKongTodayCount += haBean.getPosition().getToday_position();
                    }
                }

            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setResult(0, null);
        finish();
    }
}
